package cn.tedu.d2_thread;
/*
 线程结束方式,三种
 */
public class ThreadDemo01 {
    public static int count=0;
    static boolean flag=true;//标识
    public static void main(String[] args) {

//        int count=0;报错
//        设置成员变量，设置成静态
        Object obj=new Object();

        Thread t=new Thread(){
            public void run() {
              /*
               线程任务
               每隔一秒，输出整数
               */
                while (flag) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj) {
                        System.out.println(++count);
                    }
                }
            }
        };
        t.start();

        //当输出5以后结束线程
        while (true){

//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            synchronized (obj) {
                //System.out.println("main:"+count);
                if (count >= 5) {
                    flag = false;
                    break;
                }
            }

        }

        System.out.println(Thread.currentThread().getName()+",运行结束");
    }

}
